home *** CD-ROM | disk | FTP | other *** search
- Path: uunet!caen!uakari.primate.wisc.edu!zazen!mis.mcw.edu!tenaglia
- From: tenaglia@mis.mcw.edu
- Newsgroups: vmsnet.sources.games
- Subject: TICK TACK TOE (1/1)
- Message-ID: <1991Dec20.100054.122@mis.mcw.edu>
- Date: 20 Dec 91 15:00:54 GMT
- Organization: Medical College of Wisconsin MIS Department
- Lines: 257
-
-
- Enclosed is the source for a really dumb version of Tick Tack Toe. It's
- written in the Icon Programming language which is public domain and
- available for VMS, unix, dos, amiga, IBM MAINFRAM, MACintosh, etc,...
- from the University of Arizona (Icon-Project@cs.arizona,edu).
-
- Why am I posting a 'dumb version'? Because it wins more often than it
- deserves to (it cheats), and there's no 'explicit' logic implementing
- the strategy. The strategy is isomorphic, a side effect of slightly
- careless/sloppy coding. I've tested it under VMS and Ultrix unix. I
- thought it might be interesting for some. You'll need a VT100 compatible
- display. I've been flamed in the past for this, oh well.
-
- Chris Tenaglia (System Manager) | "The past explained,
- Medical College of Wisconsin | the future fortold,
- 8701 W. Watertown Plank Rd. | the present largely appologized for."
- Milwaukee, WI 53226 | Organon to The Doctor
- (414)257-8765 |
- tenaglia@mis.mcw.edu
-
-
- ###############################################################
- # #
- # file : ttt.icn #
- # updt : 27-nov-1991 #
- # auth : chris tenaglia #
- # desc : tictactoe icon implementation #
- # note : This version cheats because of sloppy use #
- # of string and numeric compares. It actually #
- # is more fun this way. Save for posterity. #
- # #
- ###############################################################
- global me,you,true,false,draw,pointer,wins,pass,taken,winner,mark,row
- procedure main()
- init()
- play := true
- while play == true do
- {
- me := set() # computer is me
- you := set() # player is you
- victory := "" # nobodys' won yet
- pass := 0 # start flag
- winner := ""
- taken := table(false) # taken position table (rather than set?)
- display()
- insert(me,5)
- taken[5] := true
- display()
- insert(you,(tmp := get_your_move()))
- taken[integer(tmp)] := true
- display()
- repeat
- {
- insert(me,(tmp := choose([1,2,3,4,6,7,8,9])))
- taken[integer(tmp)] := true
- display()
- if (victory := done_yet()) == (true|false|draw) then break
- insert(you,(tmp := get_your_move()))
- taken[integer(tmp)] := true
- if (victory := done_yet()) == (true|false|draw) then break
- }
- display()
- case winner of
- {
- "me" : {
- write(at(1,22),chop(&host)," Wins, You Loose!")
- every square := !row do writes(pointer[square],mark)
- }
- "you": {
- write(at(1,22),chop(),"Wow! You actually beat the Computer.")
- every square := !row do writes(pointer[square],mark)
- }
- draw : write(at(1,22),chop(),"Game was a draw.")
- }
- play := map(input(at(1,23) || "Another Game (Y/N) :")[1])
- }
- end
-
- #
- # procedure to display the current tictactoe grid and plays
- #
- procedure display()
- if (pass +:= 1) = 1 then
- {
- write(cls(),uhalf()," T I C - T A C - T O E")
- write(lhalf()," T I C - T A C - T O E")
- write(trim(center("Computer is 'O' and you are 'X'",80)))
- line := repl("q",60) ; line[21] := "n" ; line[41] := "n"
- every y := 5 to 20 do writes(at(30,y),graf("x"))
- every y := 5 to 20 do writes(at(50,y),graf("x"))
- writes(at(10,10),graf(line))
- writes(at(10,15),graf(line))
- every x := 1 to 9 do writes(pointer[x],dim(x))
- }
- every writes(pointer[!me],high("O"))
- every writes(pointer[!you],under("X"))
- end
-
- #
- # procedure to obtain a move choice from the player
- #
- procedure get_your_move()
- local yours,all_moves
- repeat {
- writes(at(5,22))
- yours := input("Enter block # (1-9) :")
- writes(at(5,23),chop())
- if not(integer(yours)) then
- {
- writes(at(5,23),beep(),"Invalid Input! Choose 1-9.")
- next
- }
- if (1 > yours) | (yours > 9) then
- {
- writes(at(5,23),beep(),"Value out of range! Choose 1-9.")
- next
- }
- if taken[integer(yours)] == true then
- {
- writes(at(5,23),beep(),"That position is already taken! Try again.")
- next
- }
- break }
- return yours
- end
-
- #
- # procedure chooses for the host a strong move
- #
- procedure choose(lst)
- local val,test
- test := 0
- repeat {
- val := ?lst
- if (test +:= 1) > 200 then write(val," Choose: infinite loop")
- if taken[integer(val)] == true then next else break }
- return val
- end
-
- #
- # procedure to test if computer has won, or the game is a draw
- #
- procedure done_yet()
- every outcome := !wins do
- {
- test := 0
- every part := !outcome do
- if member(you,part) then test +:= 1
- if test = 3 then
- {
- winner := "you"
- row := outcome
- mark := high(blink("X"))
- return true
- }
- }
- every outcome := !wins do
- {
- test := 0
- every part := !outcome do
- if member(me,part) then test +:= 1
- if test = 3 then
- {
- winner := "me"
- row := outcome
- mark := high(blink("O"))
- return true
- }
- }
- if *me + *you > 8 then
- {
- winner := draw
- return draw
- }
- return "not done yet"
- end
-
- #
- # prompts for an input from the user
- #
- procedure input(prompt)
- writes(prompt)
- return read()
- end
-
- #
- # procedures to output ansi graphics and attributes
- #
- procedure at(x,y)
- return "\e[" || y || ";" || x || "f"
- end
-
- procedure graf(str)
- return "\e(0" || str || "\e(B"
- end
-
- procedure uhalf(str)
- /str := ""
- return "\e#3" || str
- end
-
- procedure lhalf(str)
- /str := ""
- return "\e#4" || str
- end
-
- procedure high(str)
- return "\e[1m" || str || "\e[0m"
- end
-
- procedure normal(str)
- return "\e[0m" || str
- end
-
- procedure dim(str)
- return "\e[2m" || str || "\e[0m"
- end
-
- procedure under(str)
- return "\e[4m" || str || "\e[0m"
- end
-
- procedure blink(str)
- return "\e[5m" || str || "\e[0m"
- end
-
- procedure cls(str)
- /str := ""
- return "\e[2J\e[H" || str
- end
-
- procedure chop(str)
- /str := ""
- return "\e[J" || str
- end
-
- procedure beep()
- return "\7"
- end
-
- #
- # procedure to init useful global variables for later use
- #
- procedure init()
- true := "y"
- false := "n"
- draw := "?"
- &random := map(&clock,":","0")
- wins := [set([1,5,9]),set([3,5,7]),set([1,2,3]),set([4,5,6]),
- set([7,8,9]),set([1,4,7]),set([2,5,8]),set([3,6,9])]
- pointer := [at(17,7), at(37,7), at(57,7),
- at(17,12),at(37,12),at(57,12),
- at(17,17),at(37,17),at(57,17)]
- end
-
-
-
-